package net.brucecooper.mindmap.client;

import gwt.canvas.client.Canvas;

public class LabelComponent extends SimpleComponent {

	private String text;
	private String font = "sans-serif";
	private int fontSize = 16;
	private int prefHeight = fontSize+2;
	private String color = "black";
	
	private int preferredWidth = -1;

    public LabelComponent() {
		super();
	}
	
	public LabelComponent(String string) {
		this();
		setText(string);
	}
	
    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }
    
    public void setFontSize(int size) {
        this.fontSize = size;
        this.prefHeight = size+2;
    }


	public void setText(String text) {
		this.text = text;
		this.preferredWidth = -1;
	}
	public String getText() {
		return text;
	}
	
	private String getFullFont() {
	    return fontSize + "px " + font;
	}
	
	@Override
	public void paintComponent(Canvas canvas, Clip clip) {
	    canvas.setFillStyle(color);
		canvas.setFont(getFullFont());
		canvas.fillText(text, 0, getHeight()-2);
	}
	
	
	@Override
	public int getPreferredWidth(Canvas canvas) {
	    if (preferredWidth == -1) {
    		canvas.setFont(getFullFont());
    		preferredWidth =  (int) (canvas.measureText(text) + 2);
	    }
		return preferredWidth;
	}
	
	public void setPreferredHeight(int prefHeight) {
		this.prefHeight = prefHeight;
	}
	
	@Override
	public int getPreferredHeight(Canvas canvas) {
		return prefHeight;
	}

	public void setWordWrap(boolean b) {
		// TODO Auto-generated method stub
		
	}

	public void setFont(String string) {
		this.font = string;
        this.preferredWidth = -1;
	}

    public int getFontSize() {
        return fontSize;
    }

}
